/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.action;

import com.myapp.facade.Facade;
import com.myapp.pojo.Arquivos;
import com.myapp.pojo.Usuario;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author samuel
 */
public class DeleteAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String CAMINHO_SERVIDOR = Facade.getInstance().buscarConfiguracao().getCaminho();

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        List<Arquivos> arquivos = Facade.getInstance().buscarArquivos();
        request.getSession().setAttribute("arquivoLista", arquivos);

        String select[] = request.getParameterValues("codigoSelecao");

        if (select != null) {
            String msg = "";
            for (int i = 0; i < select.length; i++) {
                msg += select[i] + ", ";
                File f = new File(CAMINHO_SERVIDOR + select[i]);
                f.delete();
                Facade.getInstance().deletaArquivo(CAMINHO_SERVIDOR + select[i]);
            }
            ActionMessages messages = new ActionMessages();
            messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.checkbox", msg));
            saveMessages(request, messages);
        }

        return mapping.findForward(SUCCESS);
    }
}
